﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace UcAsp.WorkFrame
{
    public class Parser
    {
        public  static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        //信息写入WINDOWS日志
        public static void ErrorLog(string sourceName, string message, bool isError)
        {
            try
            {
                System.Diagnostics.EventLog eventLog = null;
                // 确定日志是否存在
                if (!(System.Diagnostics.EventLog.SourceExists(sourceName)))
                {
                    System.Diagnostics.EventLog.CreateEventSource(sourceName, sourceName + "Log");
                }
                if (eventLog == null)
                {
                    eventLog = new System.Diagnostics.EventLog(sourceName + "Log");
                    eventLog.Source = sourceName;
                }

                // 记录日志信息
                if (isError)
                {
                    eventLog.WriteEntry(message, System.Diagnostics.EventLogEntryType.Error);
                }
                else
                {
                    eventLog.WriteEntry(message, System.Diagnostics.EventLogEntryType.Information);
                }
            }
            catch { }
        }
    }
}
